package me.khrystal.rxnews.module.video.presenter;

import com.socks.library.KLog;

import java.util.List;

import me.khrystal.rxnews.base.BasePresenterImpl;
import me.khrystal.rxnews.bean.NeteastVideoSummary;
import me.khrystal.rxnews.common.DataLoadType;
import me.khrystal.rxnews.module.video.model.IVideoListInteractor;
import me.khrystal.rxnews.module.video.model.IVideoListInteractorImpl;
import me.khrystal.rxnews.module.video.view.IVideoListView;

/**
 * usage:
 * author: kHRYSTAL
 * create time: 16/5/1
 * update time:
 * email: 723526676@qq.com
 */
public class IVideoListPresenterImpl
        extends BasePresenterImpl<IVideoListView,List<NeteastVideoSummary>>
        implements IVideoListPresenter{


    private IVideoListInteractor<List<NeteastVideoSummary>> mVideoListInteractor;

    private String mId;
    private int mStartPage;

    private boolean mIsRefresh = true;
    private boolean mHasInit;

    public IVideoListPresenterImpl(IVideoListView view,String id,int startPage ) {
        super(view);
        mId = id;
        mStartPage = startPage;
        mVideoListInteractor = new IVideoListInteractorImpl();
        mSubscription = mVideoListInteractor.requestVideoList(this,id,startPage);
    }

    @Override
    public void beforeRequest() {
        if (!mHasInit)
            mView.showProgress();
    }

    @Override
    public void requestError(String msg) {
        super.requestError(msg);
        mView.updateVideoList(null,mIsRefresh ? DataLoadType.TYPE_REFRESH_FAIL : DataLoadType.TYPE_LOAD_MORE_FAIL);
    }

    @Override
    public void refreshData() {
        mStartPage = 0;
        mIsRefresh = true;
        mSubscription = mVideoListInteractor.requestVideoList(this,mId,mStartPage);
    }

    @Override
    public void loadMoreData() {
        KLog.e("加载更多数据: " + mId + ";" + mStartPage);
        mIsRefresh = false;
        mSubscription = mVideoListInteractor.requestVideoList(this, mId, mStartPage);
    }

    @Override
    public void requestSuccess(List<NeteastVideoSummary> data) {
        KLog.e("请求成功");
        mHasInit = true;
        if (data!=null && data.size() > 0){
            mStartPage += 10;
        }
        mView.updateVideoList(data,mIsRefresh ? DataLoadType.TYPE_REFRESH_SUCCESS
            :DataLoadType.TYPE_LOAD_MORE_SUCCESS);
    }

    @Override
    public void requestComplete() {
        super.requestComplete();
    }
}
